package com.guli.edu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.edu.client.OrderClient;
import com.guli.edu.entity.Course;
import com.guli.edu.entity.form.CourseForm;
import com.guli.edu.entity.fuzzy.CourseLike;
import com.guli.edu.entity.fuzzy.CourseLikeFront;
import com.guli.edu.entity.vo.ChapterVo;
import com.guli.edu.entity.vo.CoursePublishVo;
import com.guli.edu.entity.vo.CourseWebVo;
import com.guli.edu.service.ChapterService;
import com.guli.edu.service.CourseService;
import com.guli.utils.JwtUtils;
import com.guli.utils.R;
import com.guli.utils.vo.OrderCourseWebVo;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ZhaoZhenyu
 * @since 2022-09-26
 */

//@CrossOrigin
@Api(description="课程管理")
@RestController
@RequestMapping("/edu/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    // 添加课程
    @PostMapping("/addCourse")
    public R addCourse(@RequestBody CourseForm courseForm){
        String courseId = courseService.saveCourse(courseForm);
        if (StringUtils.hasText(courseId)) {
            return R.ok().data("courseId", courseId);
        }else {
            return R.error().message("保存课程失败");
        }
    }

    // 分页查询带模糊
    @GetMapping("/coursePageLikeQuery/{current}/{size}")
    public R coursePageLikeQuery(@PathVariable Integer current, @PathVariable Integer size,
                                     CourseLike courseLike){
        // 分页：new Page<>(c,l)、selectPage(Page<>,QueryWrapper)
        Page<Course> PageParam = new Page<>(current, size);
        courseService.getCourseByLikePage(PageParam,size,courseLike);
        List<Course> records = PageParam.getRecords();
        long total = PageParam.getTotal();
        return R.ok().data("records",records).data("total",total);
    }

    @GetMapping("/showCourseForm/{id}")
    public R showCourseFormById(@PathVariable String id){
        CourseForm courseForm = courseService.getCourseFormById(id);
        return R.ok().data("item",courseForm);
    }

    @PutMapping("/updateCourseForm/{id}")
    public R updateCourseFormById(@PathVariable String id,@RequestBody CourseForm courseForm){
        boolean flag = courseService.updateCourseFormById(id,courseForm);
        if (flag){
            return R.ok();
        }else {
            return R.error().message("更新课程失败");
        }
    }

    // 删除课程
    @DeleteMapping("/deleteCourse/{id}")
    public R deleteCourse(@PathVariable String id){
        // 逻辑删除 就先不删除 章节 和 视频
        boolean flag = courseService.removeById(id);
        if(flag){
            return R.ok();
        }else{
            return R.error().message("删除课程失败");
        }
    }

    // 发布页展示数据
    @GetMapping("/showCoursePublish/{id}")
    public R showCoursePublish(@PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.getCoursePublishVoById(id);
        return R.ok().data("item", coursePublishVo);
    }

    // 发布课程  即是更改course表中status状态
    @PutMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        courseService.publishCourseById(id);
        return R.ok();
    }

    @PostMapping("/coursePageLikeOfFront/{page}/{limit}")
    public R coursePageLikeOfFront( @PathVariable Long page,@PathVariable Long limit,
            @RequestBody(required = false) CourseLikeFront courseLikeFront){
        Page<Course> pageParam = new Page<>(page, limit);
        Map<String, Object> map = courseService.getCourseByPageLikeOfFront(pageParam, courseLikeFront);
        return  R.ok().data(map);
    }

    // 多表查询course信息
    @GetMapping(value = "/courseInfoOfFront/{courseId}")
    public R getById(@PathVariable String courseId, HttpServletRequest request){

        //查询课程信息和讲师信息
        CourseWebVo courseWebVo = courseService.getInfoWebById(courseId);

        //查询当前课程的章节信息 含有 章节 和 视频
        List<ChapterVo> chapterVoList = chapterService.nestedListChapterByCourseId(courseId);

        // 远程调用order模块 查询课程支付状态
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean isBuyCourse = orderClient.isBuyCourse(memberId, courseId);

        return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVoList).data("isBuyCourse",isBuyCourse);
    }

    //根据课程id查询课程信息
    @GetMapping("courseInfoProvider/{courseId}")
    public OrderCourseWebVo getCourseInfoDto(@PathVariable String courseId) {
        //查询课程信息和讲师信息
        CourseWebVo courseWebVo = courseService.getInfoWebById(courseId);
        OrderCourseWebVo orderCourseWebVo = new OrderCourseWebVo();
        BeanUtils.copyProperties(courseWebVo,orderCourseWebVo);
        return orderCourseWebVo;
    }

}

